#include "omronwidget.h"
#include "ui_omronwidget.h"

OmronWidget::OmronWidget(QString ParentName, QString MyName, QWidget *parent )
  : QWidget( parent ),
    ui( new Ui::OmronWidget )
{
    ui->setupUi(this);
    MyNameIs = MyName;
    WhoAmI = ParentName+MyName;
    InitSettings = new QSettings("Init2.ini",QSettings::IniFormat);
    InitSettings->setIniCodec( "utf8" );
    QString PropName = WhoAmI + "/Prefix";
    QString Name = InitSettings->value(PropName).toString();
    ui->Prefix->setText(Name);
    PropName = WhoAmI + "/Suffix";
    Name = InitSettings->value(PropName).toString();
    ui->Suffix->setText(Name);
    PropName = WhoAmI + "/MemType";
    MemType = InitSettings->value(PropName).toString();
    PropName = WhoAmI + "/MemSize";
    MemSize = InitSettings->value(PropName).toInt();
    PropName = WhoAmI + "/MemAddr";
    MemAddr = InitSettings->value(PropName).toInt();

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(RefreshFromFins()));
    timer->start(100);

    setLayout( ui->horizontalLayout );
    ui->Line->installEventFilter(this);
    WhereFins =  new Fins;


    signalMapper = new QSignalMapper(this);
    connect(this, SIGNAL(nextFieldSig()), signalMapper, SLOT(map()));
    signalMapper->setMapping(this, MyNameIs);
    connect(signalMapper, SIGNAL(mapped(const QString &)), parent, SLOT(nextField(const QString &)));
    connect(this, SIGNAL(prevFieldSig()), signalMapper, SLOT(map()));

    signalMapper2 = new QSignalMapper(this);
    connect(this, SIGNAL(prevFieldSig()), signalMapper2, SLOT(map()));
    signalMapper2->setMapping(this, MyNameIs);
    connect(signalMapper2, SIGNAL(mapped(const QString &)), parent, SLOT(prevField(const QString &)));
    //connect(this, SIGNAL(nextFieldSig()), parent, SLOT(nextField()) );
    //connect(this, SIGNAL(prevFieldSig()), parent, SLOT(prevField()) );
}

void OmronWidget::setValueI( int Value )
{
    _value = Value;
    ui->Line->setText(QString::number(_value));
}

void OmronWidget::setValue( QString Value )
{
    ui->Line->setText(Value);
}


OmronWidget::~OmronWidget()
{
    delete ui;
}


bool OmronWidget::eventFilter(QObject *obj, QEvent *e)
{
    return QObject::eventFilter(obj, e);
}

void OmronWidget::RefreshFromFins ()
{

    QString sss;
    if(MemAddr < 512)
    {
        sss= QString::number(WhereFins->GetPLCMemW(MemAddr));
    }
    else
        sss = "NoData";
    ui->Line->setText(sss);
}

void OmronWidget::SetWhereFins(Fins *addr)
{
    WhereFins = addr;
}



